//
//  STChannelCardCell.swift
//  StarTimesON
//
//  Created by Toj on 9/26/22.
//  Copyright © 2022 StarTimes. All rights reserved.
//

import UIKit

protocol STChannelCardCellDelegate: NSObjectProtocol {
    func channelCardCellDidTapSelected(_ view: STChannelCardCell)
    func channelCardCellDidTapAddCard(_ view: STChannelCardCell)
}

class STChannelCardCell: UICollectionViewCell {
    
    weak var delegate: STChannelCardCellDelegate?
    
    var model: STChannelCellModel? { didSet {
        guard let channel = model else { return }
        //print("-->toj: isCarCell: \(channel.isCarCell)")
        backView.isHidden = channel.isAddCarCell
        addCardButton.isHidden = !backView.isHidden
        
        if backView.isHidden { return }
        
        nameLabel.text = channel.name
        selectedButton.isSelected = channel.isSelected
        setupRelayoutDiscount()
        
        if channel.isCarCell {
            iconImageView.image = UIImage(resourceName: channel.cardIcon)
            return
        }
        let url = URL(string: channel.iconUrl)
        iconImageView.kf.setImage(with: url, placeholder: "pay_ment_default".to_image)
    } }
    
    @objc private func didTapAddCard() {
        delegate?.channelCardCellDidTapAddCard(self)
    }
    
    @objc private func didTapSelected() {
        delegate?.channelCardCellDidTapSelected(self)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupAddSubViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        setupLayoutSubViews()
    }
    
    private lazy var leftShadowView: UIImageView = {
        let view = UIImageView()
        view.image = "commodity_channel_shadow_left".to_image
        return view
    }()
    
    private lazy var rightShadowView: UIImageView = {
        let view = UIImageView()
        view.image = "commodity_channel_shadow_right".to_image
        return view
    }()
    
    private lazy var backView: UIView = {
        let view = UIView()
        let tap = UITapGestureRecognizer(target: self, action: #selector(didTapSelected))
        view.addGestureRecognizer(tap)
        return view
    }()
    
    private lazy var iconImageView: UIImageView = {
        let view = UIImageView()
        view.contentMode = .scaleAspectFit
        return view
    }()
    
    private lazy var nameLabel: UILabel = {
        let view = UILabel()
        view.font = UIFont.bold(with: 12.ui_scale)
        view.textColor = "#666666".to_color
        return view
    }()
    
    private lazy var selectedButton: UIButton = {
        let view = UIButton()
        view.setImage(UIImage(resourceName: "com_radio_gold_nor"), for: .normal)
        view.setImage(UIImage(resourceName: "com_radio_gold_sel"), for: .selected)
        view.isUserInteractionEnabled = false
        return view
    }()
    
    private lazy var lineView: UIImageView = {
        let view = UIImageView()
        view.image = "commodity_channel_cell_line".to_image
        return view
    }()
    
    private let addCardButtonHeight = 28.ui_scale
    private lazy var addCardButton: UIButton = {
       let view = UIButton()
        view.addTarget(self, action: #selector(didTapAddCard), for: .touchUpInside)
        return view
    }()
    
    private lazy var addCardImageView: UIImageView = {
        let view = UIImageView()
        view.image = "commodity_channel_addCard".to_image
        return view
    }()
    
    private lazy var addCardLabel: UILabel = {
        let view = UILabel()
        view.textColor = "#666666".to_color
        view.font = UIFont.boldSystemFont(ofSize: 12.f_scale)
        view.text = "ADD A CARD TO PAY"
        return view
    }()
    
    private lazy var addCardArrowsIcon: UIImageView = {
        let view = UIImageView()
        view.image = "ic_right_def_g".to_image
        return view
    }()
}

extension STChannelCardCell {
    
    private func setupAddSubViews() {
        contentView.addSubview(leftShadowView)
        contentView.addSubview(rightShadowView)
        contentView.addSubview(backView)
        contentView.addSubview(lineView)
        contentView.addSubview(addCardButton)
        
        backView.addSubview(iconImageView)
        backView.addSubview(nameLabel)
        backView.addSubview(selectedButton)
        
        addCardButton.addSubview(addCardImageView)
        addCardButton.addSubview(addCardLabel)
        addCardButton.addSubview(addCardArrowsIcon)
    }
    
    private func setupLayoutSubViews() {
        backView.snp.makeConstraints { make in
            make.edges.equalTo(contentView)
        }
        
        leftShadowView.snp.makeConstraints { make in
            make.left.equalTo(contentView).inset(7)
            make.width.equalTo(49)
            make.top.bottom.equalTo(contentView)
        }
        
        rightShadowView.snp.makeConstraints { make in
            make.right.equalTo(contentView).inset(7)
            make.width.top.bottom.equalTo(leftShadowView)
        }
        
        iconImageView.snp.makeConstraints { make in
            make.left.equalTo(58.ui_scale)
            make.centerY.equalTo(contentView)
            make.width.equalTo(24)
            make.height.equalTo(24)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(iconImageView.snp.right).offset(6.ui_scale)
            make.centerY.equalTo(contentView)
        }
        
        selectedButton.snp.makeConstraints { make in
            make.right.equalTo(contentView).inset(28.ui_scale)
            make.centerY.equalTo(contentView)
            make.width.equalTo(20)
            make.height.equalTo(20)
        }
        
        // 添加卡
        addCardButton.snp.makeConstraints { make in
            make.left.equalTo(contentView).inset(58.ui_scale)
            make.right.equalTo(contentView)
            make.centerY.equalTo(contentView)
            make.height.equalTo(addCardButtonHeight)
        }
        
        addCardImageView.snp.makeConstraints { make in
            make.left.centerY.equalTo(addCardButton)
            make.width.equalTo(16)
            make.height.equalTo(16)
        }
        addCardLabel.snp.makeConstraints { make in
            make.left.equalTo(addCardImageView.snp.right).offset(4.ui_scale)
            make.centerY.equalTo(contentView)
        }
        addCardArrowsIcon.snp.makeConstraints { make in
            make.centerY.equalTo(addCardButton)
            make.right.equalTo(addCardButton).inset(30.ui_scale)
            make.width.equalTo(9)
            make.height.equalTo(16)
        }
        
        setupRelayoutDiscount()
    }
    
    private func setupRelayoutDiscount() {
        let left = backView.isHidden ? 28.ui_scale : 56.ui_scale
        lineView.snp.remakeConstraints { make in
            make.left.equalTo(left)
            make.height.equalTo(1)
            make.right.equalTo(contentView).inset(28.ui_scale)
            make.bottom.equalTo(contentView)
        }
    }
}
